package com.weicoder.shop.action;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import com.weicoder.common.constants.StringConstants;
import com.weicoder.common.lang.Conversion;
import com.weicoder.common.util.EmptyUtil;
import com.weicoder.pay.Pay;
import com.weicoder.pay.bean.PayBean;
import com.weicoder.pay.bean.TradeBean;
import com.weicoder.pay.builder.PayBuilder;
import com.weicoder.shop.po.Orders;
import com.weicoder.shop.po.Receiver;
import com.weicoder.shop.po.Trolley; 
import com.weicoder.shop.po.User;
import com.weicoder.site.action.SiteAction;

/**
 * 商店Action
 * @author WD
 * @since JDK7
 * @version 1.0 2012-08-15
 */
@Controller
@Scope(WebApplicationContext.SCOPE_REQUEST)
public class ShopAction extends SiteAction<User> {
	// 支付构造器
	@Resource
	private PayBuilder	builder;
	// 收货地址
	private Receiver	receiver;
	// 订单
	private Orders		order;

	/**
	 * 添加订单
	 */
	@Transactional
	public String orderAdd() throws Exception {
		// 添加的实体和收货地址为空
		if (receiver == null || order == null) {
			return ERROR;
		}
		// 处理收货地址
		if (receiver.getId() == 0) {
			// 添加
			service.insert(receiver);
		}
		order.setReceiverId(receiver.getId());
		service.insert(add(order));
		// 清空自己的购物车购物车
		Trolley trolley = new Trolley();
		trolley.setUserId(token.getId());
		service.delete(service.list(trolley, -1, -1));
		// 返回成功
		return SUCCESS;
	}

	/**
	 * 支付订单
	 */
	@Transactional
	public String orderPay() throws Exception {
		// 判断有订单
		if (order != null && !EmptyUtil.isEmpty(order.getSn())) {
			// 查询出自己的订单
			order = service.get(Orders.class, order.getKey());
			// 是自己的订单
			if (order.getUserId() == token.getId()) {
				// 配置支付属性
				PayBean payment = new PayBean();
				payment.setBody(StringConstants.EMPTY);
				payment.setNo(order.getSn());
				payment.setSubject(StringConstants.EMPTY);
				payment.setTotal(order.getTotal());
				// 获得支付器
				Pay pay = builder.build(order.getPay());
				// 在线支付
				response.sendRedirect(pay.pay(request, response, payment));
			}
		}
		// 返回成功
		return SUCCESS;
	}

	/**
	 * 在线支付回调
	 */
	@Transactional
	public String orderTrade() throws Exception {
		// 获得支付器
		Pay pay = builder.build(Conversion.toInt(key));
		// 获得支付订单号
		TradeBean trade = pay.trade(request, response);
		// 是否支付成功
		if (trade.isSuccess()) {
			// 查询出自己的订单
			order = service.get(Orders.class, trade.getNo());
			// 更新成功
			order.setStatus(1);
			service.update(order);
		}
		return SUCCESS;
	}

	/**
	 * 获得订单
	 * @return 订单
	 */
	public Orders getOrder() {
		return order;
	}

	/**
	 * 设置订单
	 * @param order
	 */
	public void setOrder(Orders order) {
		this.order = order;
	}

	/**
	 * 获得收货地址
	 * @return 收货地址
	 */
	public Receiver getReceiver() {
		return receiver;
	}

	/**
	 * 设置收货地址
	 * @param receiver 收货地址
	 */
	public void setReceiver(Receiver receiver) {
		this.receiver = receiver;
	}
}
